package io.renren.modules.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.R;
import io.renren.common.utils.WxUtils;
import io.renren.modules.shop.entity.GoodsOrder;
import io.renren.modules.shop.entity.WxEntity;
import io.renren.modules.shop.mapper.GoodsOrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.modules.shop.mapper.WxUserMapper;
import io.renren.modules.shop.entity.WxUser;
import io.renren.modules.shop.service.WxUserService;
import org.springframework.util.StringUtils;

@Service
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements WxUserService {

    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @Override
    public int updateBatch(List<WxUser> list) {
        return baseMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<WxUser> list) {
        return baseMapper.batchInsert(list);
    }

    @Override
    public WxUser login(String js_code) {
        WxEntity wxEntity = WxUtils.checkWx(js_code);
        if ("".equals(wxEntity.getOpenid())) {
            return null;
        }
        WxUser wxUser = baseMapper.selectOne(new QueryWrapper<WxUser>().lambda().eq(WxUser::getOpenid, wxEntity.getOpenid()));
        // 判断是否存在用户，不存在则创建
        if (wxUser == null) {
            wxUser = new WxUser();
            wxUser.setOpenid(wxEntity.getOpenid());
            baseMapper.insert(wxUser);
        }
        if (wxUser.getStatus() == 1) {
            return null;
        }
        return  wxUser;
    }
}





